<?php
namespace Common\Model;
use Think\Model;
use Home\Service\ClockTimeService;
// +----------------------------------------------------------------------
// | Date:2017年3月14日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ApplyModel extends Model{
    
    
    
    
    /**
     * 申请计算
     * @param unknown $userInfo
     * @param unknown $vacation
     * @param unknown $post
     */
    public function apply_submit($userInfo,$vacation,$post){
        $this->startTrans();
        $approverModel = D('Approver');
        do{
            
            //重复申请检查
            $map_apply_exist = $post+['sn'=>$userInfo['sn']];
            $map_apply_exist['status'] = 0;
            $isApply = $this->where($map_apply_exist)->find();
            if ($isApply) {
                $this->error = '请勿重复提交申请！！';
                break;
            }
            $add = [
                'uid'=>$userInfo['id'],
                'sn'=>$userInfo['sn'],
                'name'=>$userInfo['name'],
                'total_min'=>$vacation['total_min'],
                'total_day'=>$vacation['total_day'],
                'dep_id'=>$userInfo['dep_id'],
                'dep_name'=>$userInfo['dep_name'],
                'created_time'=>CURRENT_TIME,
                'status'=>0,
                'leader_uid'=>$userInfo['leader_id'],
                'img_path'=>$post['resaon_img'],
            ];
            $add = array_merge($post,$add);
            $applyId = $this->add($add);
            
            
            if (!$applyId) {
                $this->error = '网络异常,申请记录提交失败!';
                break;
            }
            /*添加审核人记录*/
            if ($post['type']==2) {
                //假期类型
                $ret = $approverModel->approver_vacation($applyId,$userInfo,$vacation['total_day'],$post);//假期类型
            }elseif($post['type']==1){
                //外出类型
                $ret = $approverModel->approver_leave($applyId,$userInfo,$vacation['total_day'],$post);//假期类型
                
            }else{
                //加班
                $ret = $approverModel->approver_overtime($applyId,$userInfo,$add);//加班，外出类型
            }
            if (!$ret) $this->error = $approverModel->getError();
        }while(false);  
        
        if ($this->error){
            $this->rollback();
            return false;
        }
        
        $this->commit();
        return true;
    }
    
    
    /**
     * 获取该员工的申请数据的起止时间，用户传入给clocktimeService的set_apply_time方法
     * 审核通过的数据
     * @param unknown $sn
     * date:2018年3月27日
     * author: EK_熊
     */
    public function get_time_list($appInfo,$aply_type=''){
        $date = date('Y-m-d',(strtotime($appInfo['start_time'])));
        
        $day = ["$date 00:00:00","$date 23:59:59"];
        $map['status'] = 1;//必须是通宵审批的申请
        $map['sn'] = $appInfo['sn'];
        $map['start_time'] = ['between',$day];
        $map['end_time'] = ['between',$day];
        $map['id'] = ['neq',$appInfo['id']];
        if ($aply_type) $map['vacate_type'] = ['in',$aply_type];
        $list = $this->where($map)->field('id,vacate_type,start_time,end_time')->select();
        $list[] = $appInfo;
        if ($list){
            foreach ($list as $v){
                $data[] = [
                    'interval'=>[$v['start_time'],$v['end_time']],
                    'label'=>$v['vacate_label'],
                ];
            }
        }
        return $data;
    }
    
    /**
     * 打卡时获取到的申请数据，功能和上面的一样，只不过这个适用于打卡
     * @param unknown $sn
     * @param unknown $date
     * @return boolean[]|unknown[]|mixed[]|void[]|NULL[]|unknown[][]
     * date:2018年3月30日
     * author: EK_熊
     */
    public function get_time_list_att($sn,$time){
        $date = date('Y-m-d',strtotime($time));
        
        $day = ["$date 00:00:00","$date 23:59:59"];
        $map['status'] = 1;//必须是通宵审批的申请
        $map['sn'] = $sn;
        $map['start_time'] = ['between',$day];
        $map['end_time'] = ['between',$day];
        
        $list = $this->where($map)->field('id,vacate_type,start_time,end_time')->select();

        if ($list){
            foreach ($list as $v){
                $evectionCfg = get_vacation_name_en($v['vacate_type']);
                $data[] = [
                    'interval'=>[$v['start_time'],$v['end_time']],
                    'label'=>$evectionCfg,
                ];
            }
        }
        return $data;        
    }
    
    
    
    
    /**
     * 获取待审核状态的申请时间
     * @param unknown $sn
     * @param unknown $start
     * @param unknown $end
     * date:2017年8月29日
     * author: EK_熊
     */
    public function min_wait_in_apply($sn,$vacatetype,$start='',$end=''){
        //默认获取前60天的数据      
        if (empty($start) && empty($end)) {
            $lastday_end = date('Y-m-d', strtotime("-1 day"));
            $start_from_60 = date("Y-m-d",strtotime("-60 day"));//60天以前
            $map_aply['date']=['between',[$start,$end]];
        }
        
        $map_aply['vacate_type'] =$vacatetype;
        $map_aply['status'] =0;
        $map_aply['sn'] =$sn;
        
        $overtime_aply = $this->where($map_aply)->sum('total_min');
        return !$overtime_aply ? 0 : $overtime_aply;
    }
    
    
    
}